require(['util', 'validator'], function () {
        var $improvedCommentForm = $("#improvedCommentForm"),
            corpId = "",
            seleObj = {},
            codeValue = "",
            codeDsc = "";

        //$improvedCommentForm.hide();

        //左侧树点击事件
        commonFun.leftTreeNodeClickObj['funCheckBehaviorImprovedCommentSet'] = function(event, treeId, treeNode) {
            if(treeNode.type === 'CAR' || treeNode.type === 'DRIVER'){
                $.messager.alert("提示","请选择组织机构或线路，进行参数设置。","info");
                return;
            }
            seleObj = treeNode;
            corpId = treeNode.corpId;
            initLeft();
        };

        //根据后台返回的数据，动态生成界面
        var initRight = function(){
            $.ajaxExt({
                url : "/driverComment/searchDriverCommentInfo.htm",
                data : {"corpId":corpId, "organId":seleObj.id, "qcommentTypeCodes":new Array("CMT10000","CMT20000").toString(), "qcodeValue":codeValue}
            }).done(function(data){
                $('#showData').empty();
                $('#showData').removeClass("select-tree-warning");
                if(data){
                    var currData = data;

                    //初始化界面
                    $('#improvedCommentBnt').show();
                    $('#showData').append("<h1 class='line-title'>【"+seleObj.name+"】的改进评语设置</h1>");

                    //根据数据渲染界面
                    var seleObjId = seleObj.id,
                        currObjId = currData[0].organId;

                    $('#showData').append("<input type='hidden' name='seleId' value='"+seleObjId+"'/>"
                    +"<input type='hidden' name='corpId' value='"+corpId+"'/>"
                    +"<input type='hidden' name='currId' value='"+currObjId+"'/>");
                    var commentGradeDsc = "";
                    $.each(currData,function(n,entity){
                        var params = window.parent.GLOBAL_PARAMS.DICT_CACHE[entity.paramCode];
                        var paramDsc = "";
                        $.each(params,function(n,param){
                            paramDsc = paramDsc + param.value + ", ";
                        });
                        paramDsc = paramDsc.substr(0,paramDsc.length-2);

                        var preview = entity.commentCntnt.format({'0':'驾驶技能'});

                        commentGradeDsc = entity.commentGradeDsc;
                        if(!entity.commentGradeDsc){
                            commentGradeDsc = "";
                        }

                        var content = "<div class='user-info-box'>"
                            +  "<input type='hidden' name='commentIds' value='"+entity.commentId+"'/>"
                            +"<h2 class='title-style1'><span>评语等级："+entity.commentGrade+"</span></h2>"
                            + "<ul class='form-grid form-grid2 form-grid-nowrap clearfix'>"
                            + "<li>"
                            +"<label class='label-text'>评语名称：</label><input type='text' name='commentGradeDscs' class='input-text' maxlength='16' style='width:90px;' data-validator='{required:true}' placeholder='请输入评语名称' value='"+commentGradeDsc+"'/>"
                            + "</li>"
                            + "<li>"
                            +"<label class='label-text'>等级范围：</label><input type='text' name='beginValues' class='input-text' style='width:100px;' data-validator='{required:true,number:true}' placeholder='请输入开始值' value='"+entity.beginValue+"'/> ~ <input type='text' name='endValues' class='input-text' style='width:100px;' data-validator='{required:true,number:true}' placeholder='请输入结束值' value='"+entity.endValue+"'/>"
                            + "</li>"
                            + "<li class='li-textarea mb0'>"
                            + "<label class='label-text vt'>评语内容：</label><textarea class='vt' name='commentCntnts' rows='3' cols='80' data-validator='{required:true}' placeholder='根据参数，设置评语内容。'>"+entity.commentCntnt+"</textarea>"
                            + "</li>"
                            + "<li class='mt5 h-auto'><label class='label-text'>参数说明：</label>"
                            + "<span class='span-text c-999'>" + paramDsc + "</span>"
                            + "</li>"
                            + "<li class='mb0 mt10 h-auto wrap' style='width: 800px;'>"
                            + "<label class='label-text'>预览效果：</label><span class='comment-preview'>" + preview + "</span>"
                            + "</li>"
                            + "<li class='mt5 h-auto'><label class='label-text'>备注：</label>"
                            + "<input type='text' name='remarks' class='input-text' style='width:498px;' maxlength='32' placeholder='输入备注信息。' value='"+(entity.remark==null?"":entity.remark)+"'/>"
                            + "</li>"
                            + "</ul>";
                            + "</div>"
                        $('#showData').append(content);
                    });
                    $('textarea[name=commentCntnts]').on("change", function () {
                        var preview = $(this).val().format({'0':'驾驶技能'});
                        $(this).parent().parent().find(".comment-preview").html(preview);
                    });
                }else{
                    //初始化界面
                    $('#improvedCommentBnt').hide();
                    $('#showData').addClass("select-tree-warning");
                    $('#showData').html("无数据。");
                }
            });
        };

        // 界面初始化
        var initLeft = function(){
            $('#u_tip').hide();
            $('#left').empty();
            $('#left').height($(window).height())
            $('#left').show();
            $('#showData').empty();
            $('#showData').addClass("select-tree-warning");
            $('#showData').html("选择左侧的异常行为，设置对应的改进评语。");
            $('#showData').show();
            $('#improvedCommentBnt').hide();
            $(".user-info-wrap").css("padding-left","177px")
            var leftParams = window.parent.GLOBAL_PARAMS.DICT_CACHE.DRV00000;
            $('#left').append("<h3>改进评语设置</h3>");
            $('#left').append("<ul>");
            $.each(leftParams,function(n,leftParam){
                $('#left ul').append("<li codeValue='"+leftParam.key+"' codeDsc='"+leftParam.value+"'><a href='javascript:;'>"+leftParam.value+"</a></li>");
            });
            $('#left li a').on('click', function(){
                $('#left li a').removeClass();
                $(this).addClass('active');
                codeValue = $(this).parent().attr('codeValue');
                codeDsc = $(this).parent().attr('codeDsc');
                // console.log(codeValue,codeDsc);
                initRight();
            });
        };

        //更新
        $("#improvedComment_update").on("click", function(){
            if($.validator($('#improvedCommentForm'))){
                $.messager.confirm("是否修改","确定修改该机构的改进评语设置信息？",function(r){
                    if(r){
                        var formData = $improvedCommentForm.serialize();
                        $.ajaxExt({
                            url : "/driverComment/uptDriverCommentInfo.htm",
                            data : formData
                        }).done(function(data){
                            initRight();
                        });
                    }
                });
            }
        });

        //重置
        $("#improvedComment_reset").on("click", function(){
            initRight();
        });
        
        //页面打开自动查询
        var nodes = commonFun.getLeftTreeSelectedNodes();
    	if(nodes.length<1||nodes[0].type==="LINE"){
    		seleObj = nodes[0];
            corpId = nodes[0].corpId;
            initLeft();
    	}
})